package com.petsdar.service;

import com.petsdar.dao.PowerMapper;
import com.petsdar.pojo.Power;
import com.petsdar.utils.Constant;
import com.petsdar.utils.JedisUtil;
import com.petsdar.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/7/4
 * Time: 15:46
 * Description: power相关数据的业务逻辑处理接口的实现
 * 获取所有权限的业务逻辑处理；
 * 更新权限的业务逻辑处理
 * Version: V1.0
 */

@Service
public class PowerServiceImpl implements PowerService{

    /** power相关的持久化操作实例对象 */
    private PowerMapper powerMapper;
    /** 对员工信息的权限 */
    private String[] empPowerArr = {"新增","删除","修改","查询"};
    /** 对店铺信息的权限 */
    private String[] shopPowerArr = {"修改","查询"};

    @Autowired
    public void setPowerMapper(PowerMapper powerMapper) {
        this.powerMapper = powerMapper;
    }

    /**
     * 根据获取店长账户所有权限信息
     * @return
     */
    @Override
    public Map<String, Object> getKeeperPower() {
        return getPower(Constant.KEEPER_POWER_ID);
    }

    /**
     * 根据店长id获取权限。对获取到的权限对象，根据员工权限和店铺权限
     * 转换成两个字符串数组，放入map集合中返回。将员工权限和店铺权限
     * 两个Boolean数组，存入缓存中。
     * @param prId 店长账户权限id
     * @return
     */
    private Map<String, Object> getPower(String prId) {
        Power power = powerMapper.getPower(prId);
        Map<String, Object> map = new HashMap<>();
        map.put(Constant.PR_EMP_POWER,getPrEmpPowers(power));
        map.put(Constant.PR_SHOP_POWER,getPrShopPowers(power));
        boolean[] prEmpPowers = {power.isAdd(),power.isDel(),power.isUpdate(),power.isQuery()};
        boolean[] prShopPowers = {power.isShopUpdate(),power.isShopQuery()};
        setPowerCache(prEmpPowers, Constant.PR_EMP_POWER);
        setPowerCache(prShopPowers,Constant.PR_SHOP_POWER);
        return map;
    }

    /**
     * 通过权限对象，将关于员工权限的字段转换成字符串数据返回
     * @param power 权限对象
     * @return
     */
    private String[] getPrEmpPowers(Power power){
        Stack<String> empPower = new Stack<>();
        if (power.isAdd()){
            empPower.push(empPowerArr[0]);
        }
        if (power.isDel()){
            empPower.push(empPowerArr[1]);
        }
        if (power.isUpdate()){
            empPower.push(empPowerArr[2]);
        }
        if (power.isQuery()){
            empPower.push(empPowerArr[3]);
        }
        String[] prEmpPowers = new String[empPower.size()];
        int index = 0;
        for (String item : empPower) {
            prEmpPowers[index++] = item;
        }
        return prEmpPowers;
    }

    /**
     * 通过权限对象，将关于店铺权限的字段转换成字符串数据返回
     * @param power 权限对象
     * @return
     */
    private String[] getPrShopPowers(Power power){
        Stack<String> shopPower = new Stack<>();
        if (power.isShopUpdate()){
            shopPower.push(shopPowerArr[0]);
        }
        if (power.isShopQuery()){
            shopPower.push(shopPowerArr[1]);
        }
        String[] prShopPowers = new String[shopPower.size()];
        int index = 0;
        for (String item : shopPower) {
            prShopPowers[index++] = item;
        }
        return prShopPowers;
    }

    /**
     * 更新权限信息
     * @param powerJson 权限相关Json数据
     * @return
     */
    @Override
    public boolean updateKeeperPower(String powerJson) {
        return updatePower(powerJson, Constant.KEEPER_POWER_ID);
    }

    /**
     * 通过id，更新权限信息。将powerJson转换成power对象，创建更新时间。
     * 对power进行持久化操作。持久化操作成功之后，将员工权限和店铺权限
     * 两个Boolean数组，更新到缓存中。
     * @param powerJson power相关的json数据
     * @param powerId 权限id
     * @return
     */
    private boolean updatePower(String powerJson, String powerId){
        Power power = JsonUtil.toObject(powerJson, Power.class, PowerServiceImpl.class);
        power.setPrId(powerId);
        power.setPrUpdateTime(new Date());
        boolean[] prEmpPowers = {power.isAdd(),power.isDel(),power.isUpdate(),power.isQuery()};
        boolean[] prShopPowers = {power.isShopUpdate(),power.isShopQuery()};
        int count = powerMapper.updatePower(power);
        if (count == 1){
            setPowerCache(prEmpPowers, Constant.PR_EMP_POWER);
            setPowerCache(prShopPowers,Constant.PR_SHOP_POWER);
            return true;
        }
        return false;
    }

    /**
     * 将权限的boolean数组，存入到缓存中。
     * @param power 权限boolean数据
     * @param key 存入缓存中的key值
     * @return
     */
    private boolean setPowerCache(boolean[] power, String key){
        String json = JsonUtil.toJson(power, PowerServiceImpl.class);
        boolean result = JedisUtil.setKey(key, json, 1);
        if (result){
            return true;
        }
        return false;
    }

    /**
     * 通过key值从缓存中获取权限数据。并返回字符串。
     * @param key 权限key值
     * @return
     */
    public String getPowerCacheString(String key){
        String[] powerCache = getPowerCache(key);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < powerCache.length; i++) {
            sb.append(powerCache[i]+" ");
        }
        return sb.toString();
    }

    /**
     * 通过key值从缓存中获取权限数据。并返回字符串数组。
     * @param key 权限key值
     * @return
     */
    public String[] getPowerCache(String key){
        String value = JedisUtil.getValue(key, 1);
        boolean[] bool = JsonUtil.toObject(value, boolean[].class, PowerServiceImpl.class);
        ArrayList<String> list = new ArrayList<>();
        if (key.contains(Constant.PR_EMP_POWER)){
            for (int i = 0; i < bool.length; i++) {
                if (bool[i]){
                    list.add(empPowerArr[i]);
                }
            }
        }else if (key.contains(Constant.PR_SHOP_POWER)){
            for (int i = 0; i < bool.length; i++) {
                if (bool[i]){
                    list.add(shopPowerArr[i]);
                }
            }
        }
        String[] result = new String[list.size()];
        int index = 0;
        for (String item : list) {
            result[index++] = item;
        }
        return result;
    }
}
